package com.book.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.book.common.annotation.Log;
import com.book.common.annotation.group.InsertGroup;
import com.book.common.annotation.group.UpdateGroup;
import com.book.common.core.controller.BaseController;
import com.book.common.core.domain.AjaxResult;
import com.book.common.enums.BusinessType;
import com.book.common.utils.DateUtils;
import com.book.system.domain.Carousel;
import com.book.system.service.ICarouselService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Bigboss
 * @create 2024-10-19 20:04
 */
@Api(tags = "轮播图管理")
@RestController
@RequestMapping("/system/carousel")
public class SysCarouselController extends BaseController {
    private final ICarouselService carouselService;

    public SysCarouselController(ICarouselService carouselService) {
        this.carouselService = carouselService;
    }

    /**
     * 获取轮播图列表
     * @param carousel
     * @return
     */
    @ApiOperation(value = "轮播图列表，权限：system:carousel:list")
    @PreAuthorize("@ss.hasPermi('system:carousel:list')")
    @GetMapping("/list")
    public AjaxResult list(Carousel carousel)
    {
        return success(carouselService.list(new LambdaQueryWrapper<Carousel>().orderByAsc(Carousel::getSort)));
    }



    /**
     * 新增轮播图
     */
    @ApiOperation(value = "新增轮播图，权限：system:carousel:add")
    @PreAuthorize("@ss.hasPermi('system:carousel:add')")
    @Log(title = "轮播图管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated({InsertGroup.class}) @RequestBody Carousel carousel)
    {

        carousel.setCreateBy(getUsername());
        carousel.setCreateTime(DateUtils.getNowDate());
        return toAjax(carouselService.save(carousel));
    }

    /**
     * 修改轮播图
     */
    @ApiOperation(value = "修改轮播图，权限：system:carousel:edit")
    @PreAuthorize("@ss.hasPermi('system:carousel:edit')")
    @Log(title = "轮播图管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated({UpdateGroup.class}) @RequestBody Carousel carousel)
    {
        carousel.setUpdateBy(getUsername());
        carousel.setUpdateTime(DateUtils.getNowDate());
        return toAjax(carouselService.updateById(carousel));
    }

    /**
     * 删除轮播图
     */
    @ApiOperation(value = "删除轮播图，权限：system:carousel:remove")
    @PreAuthorize("@ss.hasPermi('system:carousel:remove')")
    @Log(title = "轮播图管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
       return toAjax(carouselService.removeById(id));
    }
}
